import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;

/**
 * @Author xing
 * @Description
 * @Date 2021/3/8  14:22
 **/
public class HDFS {

    FileSystem fileSystem =null;

    @Before
    public void before(){

        Configuration configuration = new Configuration();

        try {
            fileSystem = FileSystem.get(URI.create("hdfs://192.168.66.90:9000"),configuration,"root");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void put(){
        try {
            fileSystem.copyFromLocalFile(new Path("F:\\录视频\\2021\\0228\\2.富文本编辑器实现.mp4"),new Path("/2.富文本编辑器实现.mp4"));
            System.out.println("success");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void get(){
        try {
            fileSystem.copyToLocalFile(new Path("/2.富文本编辑器实现.mp4"),new Path("F:\\2.富文本编辑器实现.mp4"));
             System.out.println("ok");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void ls(){
        try {
            FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
            for(FileStatus s : fileStatuses){
               System.out.println(s.getOwner());
               System.out.println(s.getPath());
               System.out.println(s.getAccessTime());
               System.out.println(s.getBlockSize());
               System.out.println(s.getModificationTime());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void mv(){
        try {
            fileSystem.rename(new Path("/2.富文本编辑器实现.mp4"), new Path("/www/"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void lf(){
        try {
            RemoteIterator<LocatedFileStatus> locat = fileSystem.listFiles(new Path("/"), true);
            while (locat.hasNext()){
                LocatedFileStatus next = locat.next();
                System.out.println(next.getPath());
                System.out.println(next.getOwner());
                BlockLocation[] blockLocations = next.getBlockLocations();
                for(int i = 0; i < blockLocations.length;i++){
                    String[] hosts = blockLocations[i].getHosts();
                    for (String host : hosts) {
                        System.out.println(host);
                    }
                    System.out.println();
                }
                System.out.println("*********************************");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @After
    public void after(){
        try {
            fileSystem.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }





}
